package com.leetcode.partition6;

import java.util.LinkedList;
import java.util.List;

/**
 * @author `RKC`
 * @date 2021/8/17 9:54
 */
public class LC559N叉树的最大深度 {

    public int maxDepth(Node root) {
        return dfs(root);
    }

    private int bfs(Node root) {
        if (root == null) return 0;
        LinkedList<Node> queue = new LinkedList<>();
        queue.add(root);
        int depth = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                Node cur = queue.pollFirst();
                for (Node child : cur.children) {
                    queue.add(child);
                }
            }
            depth++;
        }
        return depth;
    }

    private int dfs(Node root) {
        if (root == null) return 0;
        int depth = 0;
        for (Node childNode : root.children) {
            depth = Math.max(depth, dfs(childNode));
        }
        return depth + 1;
    }

    private static class Node {
        public int val;
        public List<Node> children;

        public Node() {
        }

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    }
}
